package com.yanzhang.service.provider;

import com.yanzhang.dao.provider.ProviderDao;
import com.yanzhang.dao.provider.ProviderDaoImpl;
import com.yanzhang.pojo.Provider;
import com.yanzhang.util.DBUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yanzhang
 */
public class ProviderServiceImpl implements ProviderService {

    private ProviderDao providerDao;

    public ProviderServiceImpl() {
        providerDao = new ProviderDaoImpl();
    }

    /**
     * 增加一个供应商
     * @param provider 供应商对象
     * @return 是否成功
     */
    @Override
    public boolean addProvider(Provider provider) {
        boolean flag = false;
        Connection connection = DBUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            int result = providerDao.add(connection, provider);
            connection.commit();
            if (result > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("新增供应商失败：事务回滚");
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConnection(connection);
        }
        return flag;
    }

    /**
     * 条件查询结果集
     * @param proName 供应商名
     * @param proCode 供应商编码
     * @return 供应商对象集合
     */
    @Override
    public List<Provider> getProviderList(String proName, String proCode) {
        List<Provider> providers = new ArrayList<>();
        Connection connection = DBUtil.getConnection();
        try {
            providers = providerDao.getProviderList(connection, proName, proCode);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(connection);
        }
        return providers;
    }

    /**
     * 根据id获取供应商对象
     * @param id 主键
     * @return 供应商对象
     */
    @Override
    public Provider getProviderById(int id) {
        Provider provider = null;
        Connection connection = DBUtil.getConnection();
        try {
            provider = providerDao.getProviderById(connection, id);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.closeConnection(connection);
        }
        return provider;
    }

    /**
     * 修改供应商信息
     * @param provider 供应商对象
     * @return 是否成功
     */
    @Override
    public boolean modifyProvider(Provider provider) {
        boolean flag = false;
        Connection connection = DBUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            int result = providerDao.modify(connection, provider);
            connection.commit();
            if (result > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("修改供应商失败：事务回滚");
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConnection(connection);
        }
        return flag;
    }

    /**
     * 通过id删除供应商
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteProviderById(int id) {
        boolean flag = false;
        Connection connection = DBUtil.getConnection();
        try {
            connection.setAutoCommit(false);
            int result = providerDao.deleteProviderById(connection, id);
            connection.commit();
            if (result > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                System.out.println("删除供应商失败：事务回滚");
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        } finally {
            DBUtil.closeConnection(connection);
        }
        return flag;
    }
}
